package com.space.school.service.impl;

import com.space.school.dao.UserDao;
import com.space.school.dao.impl.UserDaoImpl;
import com.space.school.domain.User;
import com.space.school.service.UserService;
import com.space.school.utils.PageBean;

import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    UserDao userDao=new UserDaoImpl();

    @Override
    public PageBean<User> findAll(int currentPage, int pageSize) {
        int start = (currentPage - 1) * pageSize;
        List<User> list=userDao.findAll(start,pageSize);
        PageBean<User> pb=new PageBean<>();
        pb.setList(list);
//        总数据数
//        int count=userDao.count();
//        pb.setTotalCount(count);
//       每页显示数据数
        pb.setRows(pageSize);
//        当前页数
        pb.setCurrentPage(currentPage);
//        总页数
//        int totalPage=(int) Math.ceil((double) count/pageSize);
//        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public void del(int userId) {
        userDao.del(userId);
    }

    @Override
    public void update(User user) {
        userDao.update(user);
    }

    @Override
    public User find(int userId) {
        return userDao.find(userId).get(0);
    }

    @Override
    public void update_status(User user) {
        if(user.getStatus().equals("1")){
            user.setStatus("0");
        }else {
            user.setStatus("1");
        }
        userDao.update_status(user);
    }

    @Override
    public PageBean<User> search(Map<String, String[]> map,int currentPage, int pageSize) {
        int start = (currentPage - 1) * pageSize;
        List<User> list=userDao.search(map,start,pageSize);
        PageBean<User> pb=new PageBean<>();
        pb.setList(list);
//        总数据数
        int count=userDao.count(map);
        pb.setTotalCount(count);
//       每页显示数据数
        pb.setRows(pageSize);
//        当前页数
        pb.setCurrentPage(currentPage);
//        总页数
        int totalPage=(int) Math.ceil((double) count/pageSize);
        pb.setTotalPage(totalPage);


        return pb;
    }
}
